package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.AuthAccess;
import com.woniu.common.ResultCom;
import com.woniu.entity.News;
import com.woniu.mapper.NewsMapper;
import com.woniu.mapper.AdminMapper;
import jakarta.annotation.Resource;
import jakarta.websocket.server.PathParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//allowCredentials = true 和 origins = "*" 不能同时使用

@Controller
//@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
public class NewsController {

    // Spring：请帮我把一个名为 "newsMapper" 且类型是 NewsMapper 的 Bean 注入到这里。
    @Resource
    private NewsMapper newsMapper;
    
    @Resource
    private AdminMapper adminMapper;

    @ResponseBody
    @RequestMapping(value = "/news/add" , method = RequestMethod.POST)
    public ResultCom addNews(@RequestBody News news) {
        if(newsMapper.insert(news)==1){
            return  ResultCom.success("添加成功");
        } else {
            return  ResultCom.error("添加失败");
        }

    }

    @ResponseBody
    @RequestMapping(value = "/news/delete" , method = RequestMethod.DELETE)
    public ResultCom deleteNews(@RequestParam Integer id) {

           int count =  newsMapper.deleteById(id);
           if(count>0){
               return  ResultCom.success("删除成功");
           } else {
               return  ResultCom.error("删除失败");
           }
    }

    @ResponseBody
    @RequestMapping(value = "/news/detail" , method = RequestMethod.GET)
    public ResultCom DNews(@RequestParam Integer id) {
//        News newObj = newsMapper.selectById(id);
        News newObj = newsMapper.selectById2(id);
        if (newObj != null && newObj.getAid() != null) {
            // 根据aid获取管理员信息
            newObj.setAdmin(adminMapper.selectById(newObj.getAid()));
            return  ResultCom.success("获取成功", newObj);
        } else {
            return  ResultCom.error("删除成功");
        }

    }

    @ResponseBody
    @RequestMapping(value = "/news/update" , method = RequestMethod.POST)
    public ResultCom updateNews(@RequestBody News news) {
        int affectedRows = newsMapper.updateById(news);

        return  ResultCom.error("编辑成功");
    }
    // 需要登录
    @AuthAccess.RequireAuth
    @ResponseBody
    @RequestMapping(value = "/news/list" , method = RequestMethod.GET)
    public String Newslist(@RequestParam(defaultValue = "1") Integer pageNum,
                           @RequestParam(defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "name", defaultValue = "", required = false) String name) {
        Page<News> page =  new Page<>(pageNum, pageSize);
        // 2. 创建查询条件（如果不需要条件，可以直接传null）

//        它是Mybatis-Plus提供的一种类型安全的查询条件构造器
        LambdaQueryWrapper<News> queryWrapper = new LambdaQueryWrapper<>();

        // 可以添加查询条件，例如：
         queryWrapper.like(News::getName, name); // 模糊查询标题
        // queryWrapper.eq(News::getStatus, 1); // 状态为1的数据

        // 3. 执行分页查询
        Page<News> resultPage = newsMapper.selectPage(page, queryWrapper);
        // 4. 返回结果（通常返回整个分页对象，包含分页信息和数据）
        return "查询成功1，数据：" + resultPage.getRecords() +
                ", 总条数：" + resultPage.getTotal();

    }


}
